package com.geoboard.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.geoboard.ActivityInterface;
import com.geoboard.FragContainerInterface;
import com.geoboard.R;

public class BoardContainer extends Fragment 
			implements FragContainerInterface{
	
	/**** Members **************************************************/
	//private BoardTextFrag textFrag = null;
	private BoardMapFrag mapOverFrag = null;
	
	private Fragment curFragment = null;
	
	private boolean isTextView = false;
	
	/**** Methods **************************************************/
	
	private void changeFragment(boolean isTextView){
		FragmentActivity act = getActivity();
		
		if(act != null){
		
			FragmentTransaction ft = act.getSupportFragmentManager().beginTransaction();
			ft.remove(curFragment);
			ft.commit();
			
			if(isTextView){				
				curFragment = new BoardTextFrag();
			} else {							
				curFragment = new BoardMapFrag();
			}
			
			ft = getActivity().getSupportFragmentManager().beginTransaction();
			ft.replace(R.id.board_content_container,curFragment);
			ft.commit();
		}
	}

	/**** Android Callback Methods *********************************/
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		if(mapOverFrag == null){
			//textFrag = new BoardTextFrag();
			mapOverFrag = new BoardMapFrag();
			curFragment = mapOverFrag;
		}
		
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		//getActivity().getSupportFragmentManager().beginTransaction()
		//		.add(R.id.board_content_container,mapOverFrag).commit();
		
		
		return inflater.inflate(R.layout.frag_board_container, container, false);		
	}
	
	@Override
	public void onAttach(Activity activity){
		super.onAttach(activity);
	}
	
	@Override
	public void onActivityCreated (Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		
		
	}
	
	@Override
	public void onViewStateRestored (Bundle savedInstanceState){
		super.onViewStateRestored(savedInstanceState);
	}
	
	@Override
	public void onStart(){
		super.onStart();	
		
	}
	
	@Override
	public void onResume(){
		super.onResume();		
		
		changeFragment(isTextView);

		ActivityInterface ai = (ActivityInterface)getActivity();
		ai.setTabState(FragType.BOARD, isTextView);
		ai.setCurFrag(false);
	}
		
		
	
	@Override
	public void onPause(){
		super.onPause();	
		
		getActivity().getSupportFragmentManager().beginTransaction().
				remove(curFragment).commit();
	}
	
	@Override
	public void onStop(){
		super.onStop();
	}
	
	@Override
	public void onDestroyView(){
		super.onDestroyView();
		
		
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
	}
	
	@Override 
	public void onDetach(){
		super.onDetach();
	}

	/**** FragContainerInterface handlers **********************/
	@Override
	public void setIsTextView(boolean isTextView) {
		if(this.isTextView != isTextView){
			this.isTextView = isTextView;
			
			changeFragment(isTextView);
		}
		
	}
}
